// pages/me/me.js
import login from '../../model/login'
import userApi from '../../model/user'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {
      nickName: '风骚少年',
      phone: '185****0685'
    },
    open: false,
    //页脚
    navigate: [{
      text: '当前版本',
      openType: 'navigateBack'
    }],
  },
  nexJoin: function () {
    wx.navigateTo({
      url: '/pages/team/team',
    })
  },
  toPage(e) {
    const {
      item
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: item.src,
    })
  },
  toMap() {
    wx.navigateTo({
      url: '/pages/me/order/map/map',
    })
  },
  openSet() { // 用户信息设置
    this.setData({
      open: true,
    });
  },
  toOrderList: function () {
    wx.navigateTo({
      url: '/pages/me/order/order',
    })
  },
  toAddressList: function () {
    wx.navigateTo({
      url: '/pages/me/address/address',
    })
  },
  toRelease: function () {
    wx.navigateTo({
      url: '/pages/me/release/release',
    })
  },
  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail
    this.setData({
      avatarUrl,
    })
    const userInfo = wx.getStorageSync('userInfo')
    wx.setStorageSync('userInfo', {
      avatarUrl,
      nickname: userInfo.nickname
    })

  },
  formSubmit(e) {
    let user = this.data.user
    user.nickname = e.detail.value.nickname
    userApi.update(user, res => {
      if (res.code == 200) {
        wx.showToast({
          title: '操作成功',
        })
        this.setData({
          user: user
        })
        login.getInfo()
      } else {
        wx.showToast({
          title: res.msg,
        })
      }
      this.setData({
        open: false
      })
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let user = login.getUser()
    this.setData({
      user: user
    })
    wx.onThemeChange((result) => {
      this.setData({
        theme: result.theme
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let user = login.getUser()
    this.setData({
      user: user
    })
    //加载字典
    const app = getApp();
    const dictData = app.globalData.dictData;
    this.setData({
      dictMap: dictData
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})